//
//  RegisterViewController.swift
//  XMGG
//
//  Created by iDev on 6/30/17.
//  Copyright © 2017 iDev. All rights reserved.
//

import UIKit
import SVProgressHUD
import Alamofire
import SwiftyJSON

class RegisterViewController: UIViewController {
    var registerView:RegisterView!
    override func viewDidLoad() {
        super.viewDidLoad()
        registerView = RegisterView(frame: UIScreen.main.bounds)
        NotificationCenter.default.addObserver(self, selector: #selector(registerResult(info:)), name: NSNotification.Name(rawValue: "registerResult"), object: nil)
        view.backgroundColor = UIColor(patternImage: UIImage(named: "content_bgregister")!)
        view.addSubview(registerView)
        
    }
    
    func userRegister(){
        firstRespondView?.resignFirstResponder()
        navigationController?.popViewController(animated: true)
    }
    
    func userRequestRegister(){
        firstRespondView?.resignFirstResponder()
        let modle = registerView.getViewModle()
        if modle.isLeagal(){
            SVProgressHUD.show(withStatus: "注册中")
            modle.registerRequest()
        }else{
            let alert = UIAlertController(title: nil, message: "手机号或密码不合法", preferredStyle: .alert)
            alert.addAction(UIAlertAction(title: "取消", style: .cancel, handler: nil))
            self.present(alert, animated: true, completion: nil)
        }
    }
    
    func registerResult(info:Notification){
        let result = info.object as! Bool
        if result {
            SVProgressHUD.showSuccess(withStatus: "注册成功")
            userRegister()
        }else{
            UserDefaults.standard.set(nil, forKey: kNSDefaultPwd)
            SVProgressHUD.showError(withStatus: "注册失败")
        }
    }
    
    func verifyCodeReq(){
        firstRespondView?.resignFirstResponder()
        let phone = registerView.getPhone()
        let phonePattern = "^((13[0-9])|(17[0,6,7,8])|(15[^4,\\D])|(18[0,0-9])|(14[5,7]))\\d{8}$"
        let phonePredicate = NSPredicate(format: "SELF MATCHES %@", argumentArray: [phonePattern])
        if phonePredicate.evaluate(with: phone){
            getVerifyCode(phone:phone)
        }else{
            let alert = UIAlertController(title: nil, message: "手机号不正确", preferredStyle: .alert)
            alert.addAction(UIAlertAction(title: "取消", style: .cancel, handler: nil))
            self.present(alert, animated: true, completion: nil)
        }
    }
    
    func getVerifyCode(phone:String){
        firstRespondView?.resignFirstResponder()
        let parameters = [
            "req":[
                "tu_account":phone,
                "tu_email":"1769712485@qq.com"
            ]
        ]
        SVProgressHUD.show(withStatus: "获取中")
        Alamofire.request(APIURL+"api/user/verfcode", method: .post, parameters: parameters, encoding: JSONEncoding.default).response { (response) in
            guard let data = HasCommonNetWorkError(response: response) else{
                SVProgressHUD.showError(withStatus: "验证码获取失败")
                return
            }
            let json = JSON(data)

            if json["code"].int == 0 {
                SVProgressHUD.dismiss()
                self.registerView.verifyBtnDisable()
            }else{
                SVProgressHUD.showError(withStatus: "验证码获取失败")
            }
        }
    }
    
    func backBtnClick() {
        navigationController?.popViewController(animated: true)
    }
    
}
